#!/bin/sh
# Bug reports and comments to Igor.Ermolaev@intel.com
source $(dirname $(readlink -f ${BASH_SOURCE[0]:-$0}))/find_loop
loops=${TRACING_SDK_FIND_FUNC_ILPS:-0}
calls=${TRACING_SDK_FIND_FUNC_ICLS:-0}

if [ $# -eq 1 ]; then focus=$1;shift; fi #'
save_IFS=$IFS;IFS='
'
if [ $# -eq 0 ]; then
   blocks_list=( `$base/find_hotspots` )
else
   blocks_list=( `echo "$@"|sed 's/ -- /\n/g'|$base/find_hotspots -f` )
fi

echo 
for l in ${blocks_list[@]}; do
   save_IFS2=$IFS;IFS=' '
      blocks=( $l )
      weight=${blocks[0]}
      module=${blocks[1]}
      fname=${blocks[2]}
   IFS=$save_IFS2

   if [ "$fname" == '&lt;unknown(s)&gt;' ]; then continue; fi
   if [ -n "$focus" ] && [ "$fname" != $focus ]; then continue; fi
   echo Processing $fname from $(basename ${module%%:[0-9]*}) with weight: $weight
   func=func.$isa.s
   $base/get_disasm ${module%%:[0-9]*} $fname >$func
   ranges=( `cat $func | $base/get_ranges $fname:$loops:$calls` )
   status=$?
   if [ $status -eq 1 ]; then
      echo 
      echo '   'The function skipped since no exits found.
      echo 
   elif [ $status -eq 2 ]; then
      echo 
      echo '   'The function skipped since has multiple exits.
      echo 
   elif [ $status -eq 3 ]; then
      echo 
      echo '   'The function skipped since contains too much loops.
      echo '   'Try to use TRACING_SDK_FIND_FUNC_ILPS=N to ignore N loops.
      echo 
   elif [ $status -eq 4 ]; then
      echo 
      echo '   'The function skipped since contains too much calls.
      echo '   'Try to use TRACING_SDK_FIND_FUNC_ICLS=N to ignore N calls.
      echo 
   else
      for r in ${ranges[@]}; do
         echo >>$func;echo "; $fname:$loops:$calls" >>$func
         save_IFS2=$IFS;IFS=' ';range=( $r );IFS=$save_IFS2
         echo 
         echo 'First, try do whole loop tracing using the following command.'
         echo 
         echo '   'sde ${tgt:+$tgt }-pinlit -trace-mt -start-address ${range[0]} -stop-address ${range[1]}:1 -length 100000000 ${extra:+$extra }-moi $(basename $module) -- '$@'
         echo 
         echo 'If number of instructions in trace is  less than 30M, choose'
         echo 'appropriate  skip  count  for  -stop-address  to get  30-70M'
         echo 'instructions in trace.'
         echo 
         if [ "${range[0]}" != "-1" ]; then
            echo "If you'd like to skip  several  functions instances  use the"
            echo 'following command:'
            echo 
            echo '   'sde ${tgt:+$tgt }-pinlit -trace-mt -start-address ${range[0]}:1 -stop-address ${range[1]}:1 -length 100000000 ${extra:+$extra }-moi $(basename $module) -- '$@'
            echo 
         fi
         break 2
      done
      echo 
      echo '   'Warning: The function isn\'t found in the module, skipping...
      echo 
   fi
done
IFS=$save_IFS

if [ -z "$range" ]; then
   echo >&2
   echo Can\'t find appropriate hot function from profile. >&2
   echo Exact call graph needs to be built for function boundaries extraction. >&2
   echo >&2
fi
